
package my.algorithms.mcorrea.careercup;

import my.algorithms.mcorrea.datastructures.*;

/**
 *
 * @author mcorrea
 */
public class Stack3 {
	
	private int top1;
        private int top2;
        private int top3;
	private int[] array;
	private int MAX_CAPACITY = 20;
	
	public Stack3(){
		this.array = new int[MAX_CAPACITY];
		this.top1 = -1;
                this.top2 = MAX_CAPACITY;
                this.top3 = MAX_CAPACITY/2;
                
	}
	
	
	public void push1(int x){
		
            if(top1 == MAX_CAPACITY/2 ){
			System.err.println("Error: stack1 is full");
		}else{
			top1 = top1 + 1;
			array[top1] = x;
		}
		
	}
        
        public void push2(int x){
		
            if(top2+1 == top3){
			System.err.println("Error: stack2 is full");
		}else{
			top2 = top2 - 1;
			array[top2] = x;
		}
		
	}
        
        
        public void push3(int x){
		
            if(top3 +1  == top2){
			System.err.println("Error: stack3 is full");
		}else{
			top3 = top3 + 1;
			array[top3] = x;
		}
		
	}
        
	
	public int pop1(){
		
		if(top1 == -1){
			System.err.println("Error: Stack2 is empty...");
			return -1;
		}else{
			int x = array[top1];
			top1 = top1 - 1; 
			return x; 
		}
	}
	
        
        public int pop2(){
		
		if(top2 == MAX_CAPACITY){
			System.err.println("Error: Stack2 is empty...");
			return -1;
		}else{
			int x = array[top2];
			top2 = top2 + 1; 
			return x; 
		}
	}
	
        public int pop3(){
		
		if(top3 == MAX_CAPACITY/2){
			System.err.println("Error: Stack3 is empty...");
			return -1;
		}else{
			int x = array[top3];
			top3 = top3 - 1; 
			return x; 
		}
	}
        
        @Override
        public String toString(){
            String str = "[";
            for(int x: array){
                str +=x+" ";
            }
            return str+"]";           
        }
        
        
        public static void main(String args[]){
            Stack3 stack = new Stack3();
            System.out.println(stack);
            stack.push1(1);
            stack.push1(2);
            stack.push1(3);
            
            stack.push2(10);
            stack.push2(11);
            stack.push2(12);
            stack.push2(13);
            
            stack.push3(20);
            stack.push3(21);
            stack.push3(22);
            stack.push3(23);
            stack.push3(23);
            stack.push3(23);
            System.out.println(stack);
            stack.pop2();
            stack.pop2();
            System.out.println(stack);
            stack.push3(23);
            stack.push3(23);
            System.out.println(stack);
            
            
        
        
        
        }
	
	
	
	
}
